package com.ruyuan.eshop.order.mq.listener;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.ruyuan.eshop.common.constants.RocketMqConstant;
import com.ruyuan.eshop.common.enums.OrderStatusEnum;
import com.ruyuan.eshop.market.domain.request.ReleaseUserCouponRequest;
import com.ruyuan.eshop.order.dao.OrderItemDAO;
import com.ruyuan.eshop.order.domain.dto.OrderInfoDTO;
import com.ruyuan.eshop.order.domain.entity.OrderItemDO;
import com.ruyuan.eshop.order.domain.request.CancelOrderAssembleRequest;
import com.ruyuan.eshop.order.mq.producer.DefaultProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lxg
 * @Description:
 * @date 2022/4/1011:34
 */
@Component
@Slf4j
public class ReleaseAssetsListener implements MessageListenerConcurrently {
    @Autowired
    private DefaultProducer defaultProducer;

    @Autowired
    private OrderItemDAO orderItemDAO;

    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
       try {
            for (MessageExt messageExt:list){
                //1、消费到释放资产的mq
                String message=new String(messageExt.getBody());
                log.info("ReleaseAssetsConsumer message:{}", message);
                CancelOrderAssembleRequest request = JSONObject.parseObject(message, CancelOrderAssembleRequest.class);
                OrderInfoDTO orderInfoDTO=request.getOrderInfoDTO();
                // 2、发送取消订单退款请求MQ
                if (orderInfoDTO.getOrderStatus()> OrderStatusEnum.CREATED.getCode()){
                    defaultProducer.sendMessage(RocketMqConstant.CANCEL_REFUND_REQUEST_TOPIC,
                            JSONObject.toJSONString(request), "取消订单退款");
                }
                //发送释放库存
               ReleaseProductStockRequest releaseProductStockRequest= buildReleaseProductStock(orderInfoDTO,orderItemDAO);
                defaultProducer.sendMessage(RocketMqConstant.CANCEL_RELEASE_INVENTORY_TOPIC,
                        JSONObject.toJSONString(releaseProductStockRequest), "取消订单释放库存");

                // 4、发送释放优惠券MQ
                if (!Strings.isNullOrEmpty(orderInfoDTO.getCouponId())) {
                    ReleaseUserCouponRequest releaseUserCouponRequest = buildReleaseUserCoupon(orderInfoDTO);
                    defaultProducer.sendMessage(RocketMqConstant.CANCEL_RELEASE_PROPERTY_TOPIC,
                            JSONObject.toJSONString(releaseUserCouponRequest), "取消订单释放优惠券");
                }
            }
           return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
       }catch (Exception e){
           log.error("consumer error", e);
           return ConsumeConcurrentlyStatus.RECONSUME_LATER;
       }
    }

    private ReleaseUserCouponRequest buildReleaseUserCoupon(OrderInfoDTO orderInfoDTO) {
        ReleaseUserCouponRequest releaseUserCouponRequest = new ReleaseUserCouponRequest();
        releaseUserCouponRequest.setCouponId(orderInfoDTO.getCouponId());
        releaseUserCouponRequest.setUserId(orderInfoDTO.getUserId());
        releaseUserCouponRequest.setOrderId(orderInfoDTO.getOrderId());
        return releaseUserCouponRequest;
    }

    private ReleaseProductStockRequest buildReleaseProductStock(OrderInfoDTO orderInfoDTO, OrderItemDAO orderItemDAO) {
        String orderId = orderInfoDTO.getOrderId();
        List<ReleaseProductStockRequest.OrderItemRequest> orderItemRequestList = new ArrayList<>();

        //  查询订单条目
        ReleaseProductStockRequest.OrderItemRequest orderItemRequest;
        List<OrderItemDO> orderItemDOList = orderItemDAO.listByOrderId(orderId);
        for (OrderItemDO orderItemDO : orderItemDOList) {
            orderItemRequest = new ReleaseProductStockRequest.OrderItemRequest();
            orderItemRequest.setSkuCode(orderItemDO.getSkuCode());
            orderItemRequest.setSaleQuantity(orderItemDO.getSaleQuantity());

            orderItemRequestList.add(orderItemRequest);
        }

        ReleaseProductStockRequest releaseProductStockRequest = new ReleaseProductStockRequest();
        releaseProductStockRequest.setOrderId(orderInfoDTO.getOrderId());
        releaseProductStockRequest.setOrderItemRequestList(orderItemRequestList);
        return releaseProductStockRequest;
    }

    private CancelOrderReleaseProductStockRequest buildSkuList(OrderInfoDTO orderInfoDTO, OrderItemDAO orderItemDAO) {
        List<OrderItemDO> orderItemDOList=orderItemDAO.listByOrderId(orderInfoDTO.getOrderId());

        Map<String,Integer> skuMap = orderItemDOList.stream()
                .collect(Collectors.toMap(OrderItemDO::getSkuCode,OrderItemDO::getSaleQuantity));

        CancelOrderReleaseProductStockRequest request=new CancelOrderReleaseProductStockRequest();
        request.setSkuCodeMap(skuMap);
        request.setOrderId(orderInfoDTO.getOrderId());
        request.setBusinessIdentifier(orderInfoDTO.getBusinessIdentifier());
        return request;
    }
}
